import java.util.Arrays;

public class Solution {
    public int[] findErrorNums(int[] nums) {
        int[] errorNums = new int[2];
        int n = nums.length;
        Arrays.sort(nums);
        int prev = 0;
        for (int i = 0; i < n; i++) {
            int curr = nums[i];
            if (curr == prev) {
                errorNums[0] = prev; // 找到重复的数字
            } else if (curr - prev > 1) {
                errorNums[1] = prev + 1; // 找到缺失的数字
            }
            prev = curr;
        }
        // 如果最后一个数字不是 n，说明 n 是缺失的数字
        if (nums[n - 1] != n) {
            errorNums[1] = n;
        }
        // 如果没有找到重复的数字，说明重复的数字是 n
        if (errorNums[0] == 0) {
            errorNums[0] = n;
        }
        return errorNums;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1, 2, 2, 4};
        int[] result = solution.findErrorNums(nums);
        System.out.println("重复的数字是: " + result[0]);
        System.out.println("缺失的数字是: " + result[1]);
    }
}